package org.autotestcase.swing;

import java.util.Iterator;

import prefuse.Visualization;
import prefuse.action.GroupAction;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Tuple;
import prefuse.data.tuple.TupleSet;

/**
 * Switch the root of the tree by requesting a new spanning tree at the desired
 * root
 */
public class TreeRootAction extends GroupAction {
	public TreeRootAction(String graphGroup) {
		super(graphGroup);
	}

	@Override
	@SuppressWarnings("unchecked")
	public void run(double frac) {
		TupleSet focus = m_vis.getGroup(Visualization.FOCUS_ITEMS);
		if (focus == null || focus.getTupleCount() == 0) {
			return;
		}

		Graph g = (Graph) m_vis.getGroup(m_group);
		Tuple f = null;
		Iterator<Tuple> tuples = focus.tuples();
		while (tuples.hasNext() && !g.containsTuple(f = tuples.next())) {
			f = null;
		}

		if (f == null) {
			return;
		}

		if (f instanceof Node) {
			g.getSpanningTree((Node) f);
		}

	}
} // end of class TreeRootAction
